using System.Configuration; 
using System.IO; 
using System.Data.SqlClient; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Collections; 

using System;
using System.Data;
using System.Diagnostics;
using pixolut.BizBlox;


namespace BizBloxCSTests
{
    public class DBAccess  
    { 
        private string m_strConnection; 
        
        // key to the application is from the webconfig
        internal DBAccess() 
        { 
            m_strConnection = DSNManager.GetConnectionString(); 
        } // New
        
        // This method has run level transaction management. This means that if you have a group of
        // dependant SQL statements which all must be executed to create an 'entity' then they must 
        // be batched in to the command string being called.
        internal void RunSQL( string strSQL, CommandType cmdType ) 
        { 
            //  Log.Out(Log.SQLDEBUG, Me, "RUNSQL: " & strSQL)
            IDbCommand cmd=null; 
            IDbConnection conn=null;  
            
            try 
            { 
                cmd = TestProviderFactory.GetCommand( strSQL, cmdType ); 
                conn = TestProviderFactory.GetConnection( m_strConnection ); 
                cmd.Connection = conn; 
                try 
                { 
                    cmd.ExecuteNonQuery(); 
                } 
                catch ( SqlException ex ) 
                { 
                    throw new DOException( "RunSQL:Failed SQL write.", strSQL, ex ); 
                } 
            } 
            catch ( SqlException ex ) 
            { 
                throw new DOException( "RunSQL:Failed Connection.", strSQL, ex ); 
            } 
            finally 
            { 
                if ( !( conn == null ) ) 
                { 
                    conn.Close(); 
                } 
                if ( !( cmd == null ) ) 
                { 
                    cmd.Dispose(); 
                } 
            } 
        } 
        
        
        
        internal DataSet RunSQLReturnDataSet( string strSQL, CommandType cmdtype ) 
        { 
            // Log.Out(Log.SQLDEBUG, Me, "READ: " & strSQL)
            try 
            { 
                DataSet ds = new DataSet( "DataSet" ); 
                IDbDataAdapter da = null; 
                da = TestProviderFactory.GetAdapter( strSQL, m_strConnection ); 
                da.Fill( ds ); 
                return ds; 
            } 
            catch ( SqlException ex ) 
            { 
                throw new DOException( "Run SQL - ReturnDataSet.", strSQL, ex ); 
            } 
            
        } 
        
    } 
    
    
    
} 
